#-*- encoding:utf-8 -*-
import web
import markdown
from configs import template_path, partial_path
from models import settingModel

def get_session():
	return web.config.get('_session')

def get_global_configs():
	return settingModel.autloads()

def markdownToHtml(text):
	return markdown.markdown(text,["fenced_code"])

def javascript(files):
	if len(files):
		return '\n'.join('<script src="/static/js/%s.js" type="text/javascript"></script>' % item for item in files)
	return '';

def css(files):
	if len(files):
		return '\n'.join('<link rel="stylesheet" href="/static/css/%s.css" />' % item for item in files)
	return '';

#global base template
template_globals = {
	'markdown':markdownToHtml,
	'session' : get_session(),
	'globals' : get_global_configs(),
	'javascript' : javascript,
	'css' : css,
}

#partial render object which can be used in templates anywhere
partial = web.template.render(partial_path,globals=template_globals)

#render object
template_globals.update(partial=partial)

view = web.template.render(template_path,globals=template_globals,base='layout')

view_plain = web.template.render(template_path,globals=template_globals)